/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.group4.highschoolportal.loginBean;

import com.group4.controller.AdminFacade;
import com.group4.controller.ParentFacade;
import com.group4.controller.StudentFacade;
import com.group4.controller.TeacherFacade;
import com.group4.entity.Admin;
import com.group4.entity.Parent;
import com.group4.entity.Student;
import com.group4.entity.Teacher;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpSession;

/**
 *
 * @author anjonas, andake, jose, victor
 */
@ManagedBean
@RequestScoped
public class IndexControllerBean {

    @EJB
    StudentFacade sdb;
    @EJB
    TeacherFacade tdb;
    @EJB
    ParentFacade pdb;
    @ManagedProperty(value = "#{indexBackingBean}")
    private IndexBackingBean ib;

    /** Creates a new instance of IndexControllerBean */
    public IndexControllerBean() {
    }

    public IndexBackingBean getIb() {
        return ib;
    }

    public void setIb(IndexBackingBean ib) {
        this.ib = ib;
    }

    public String login() {

        if (ib.getType().equals("student")) {
            
            Student student = sdb.findByLogin(ib.getUsername(), ib.getPassword());
            System.out.println(ib.getUsername() + ":" + ib.getPassword());
            System.out.println(student);

            if (student != null) {
                FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); //endast en user inloggad samtidigt
                HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
                session.setAttribute("user", student);
                System.out.print("successlogin");
                return "successLoginStudent";
            } else {
                System.out.print("successlogin");                
                return "failedLogin";
            }
        }else if(ib.getType().equals("parent")) {
            FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); //endast en user inloggad samtidigt
            Parent parent = pdb.findByLogin(ib.getUsername(), ib.getPassword());
            System.out.println(ib.getUsername() + ":" + ib.getPassword());
            System.out.println(parent);

            if (parent != null) {
                HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
                session.setAttribute("user", parent);
                System.out.print("successlogin");
                return "successLoginParent";
            } else {
                System.out.print("successlogin");                
                return "failedLogin";
            }
        }else if(ib.getType().equals("teacher")) {
            FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); //endast en user inloggad samtidigt
            Teacher teacher = tdb.findByLogin(ib.getUsername(), ib.getPassword());
            System.out.println(ib.getUsername() + ":" + ib.getPassword());
            System.out.println(teacher);

            if (teacher != null) {
                HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(true);
                session.setAttribute("user", teacher);
                System.out.print("successlogin");
                return "successLoginTeacher";
            } else {
                System.out.print("successlogin");                
                return "failedLogin";
            }
        }
        
        return "failedLogin";
    }
}